package com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.PartnerContactDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.impl.PartnerInfoTabViewImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditPartnerContactEntryView extends EditPartnerGenericEntryView<PartnerContactDTO> {

	private PartnerContactEntryView partnerContactEntryView = (PartnerContactEntryView) getPartnerGenericEntryView();
	private TitleCommandBar titleBar = new TitleCommandBar("New PartnerContact");
	private TextBox telephoneTextBox = new TextBox();
	private TextBox mobileTextBox = new TextBox();
	private TextBox faxTextBox = new TextBox();
	private TextBox emailTextBox = new TextBox();
	private TextBox siteTextBox = new TextBox();
		
	private class SaveClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			getPartnerGenericDTO().setTelephone(telephoneTextBox.getText());
			getPartnerGenericDTO().setMobile(mobileTextBox.getText());
			getPartnerGenericDTO().setFax(faxTextBox.getText());
			getPartnerGenericDTO().setEmail(emailTextBox.getText());
			getPartnerGenericDTO().setSite(siteTextBox.getText());
			getPartnerInfoTabView().onPartnerContactEntrySaved(partnerContactEntryView, EditPartnerContactEntryView.this, getPartnerGenericDTO());
		}
	}
	
	private class CancelClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			if(partnerContactEntryView != null) 
				partnerContactEntryView.getPartnerInfoTabView().endEditPartnerContact(EditPartnerContactEntryView.this, partnerContactEntryView);
			else 
				removeFromParent();
		}
	}
	
	public EditPartnerContactEntryView(PartnerInfoTabViewImpl partnerInfoTabView, PartnerContactDTO partnerContactDTO) {
		super(partnerInfoTabView, partnerContactDTO);
		
		VerticalPanel editPartnerContactPanel = new VerticalPanel();
		initWidget(editPartnerContactPanel);
		
		titleBar.addCommand("Save", new SaveClickHandler());
		titleBar.addCommand("Cancel", new CancelClickHandler());
		editPartnerContactPanel.add(titleBar);
		
		editPartnerContactPanel.add(new Label("Telephone"));
		editPartnerContactPanel.add(telephoneTextBox);
		editPartnerContactPanel.add(new Label("Mobile"));
		editPartnerContactPanel.add(mobileTextBox);
		editPartnerContactPanel.add(new Label("Fax"));
		editPartnerContactPanel.add(faxTextBox);
		editPartnerContactPanel.add(new Label("Email"));
		editPartnerContactPanel.add(emailTextBox);
		editPartnerContactPanel.add(new Label("Site"));
		editPartnerContactPanel.add(siteTextBox);
		editPartnerContactPanel.setStyleName("partnerContactPanel");
	}
	
	public EditPartnerContactEntryView(PartnerContactEntryView partnerContactEntryView) {
		this(partnerContactEntryView.getPartnerInfoTabView(), partnerContactEntryView.getPartnerGenericDTO());
		this.partnerContactEntryView = partnerContactEntryView;
		
		titleBar.setTitle("Edit PartnerContact");
		telephoneTextBox.setText(getPartnerGenericDTO().getTelephone());
		mobileTextBox.setText(getPartnerGenericDTO().getMobile());
		faxTextBox.setText(getPartnerGenericDTO().getFax());
		emailTextBox.setText(getPartnerGenericDTO().getEmail());
		siteTextBox.setText(getPartnerGenericDTO().getSite());
	}

}
